package com.beyond.common.util

import android.graphics.drawable.Drawable
import android.util.DisplayMetrics
import androidx.annotation.*
import androidx.annotation.IntRange
import androidx.core.content.ContextCompat
import java.io.File
/**
 * @author chenshiyang
 * @time 2022/6/23 16:21
 * @name ResourceUtils.kt
 * @class describe
 */
object ResourceAppUtils {
    fun getString(stringId: Int, vararg formatArgs: Any?): String {
        return AppUtils.instance.resources.getString(stringId, formatArgs)
    }


    @ColorInt
    fun getAlphaComponent(
        @ColorRes color: Int,
        @IntRange(from = 0, to = 255) alpha: Int
    ): Int {
        return if (alpha in 0..255) {
            color and 16777215 or alpha shl 24
        } else {
            throw IllegalArgumentException("alpha must be between 0 and 255.")
        }
    }
    fun getString(stringId: Int): String {
        return AppUtils.instance.resources.getString(stringId)
    }

    /**
     * Return the drawable by identifier.
     *
     * @param id The identifier.
     * @return the drawable by identifier
     */
    fun getDrawable(@DrawableRes id: Int): Drawable? {
        return ContextCompat.getDrawable(AppUtils.instance, id)
    }

    @ColorInt
    fun getColor(@ColorRes colorRes: Int): Int {
        return ContextCompat.getColor(AppUtils.instance, colorRes)
    }

    fun getDimens(dimens: Int): Float {
        return AppUtils.instance.resources.getDimension(dimens)
    }

    fun getDisplayMetrics(): DisplayMetrics? {
        return AppUtils.instance.resources.getDisplayMetrics()
    }

    fun getFilesDir(): File? {
        return AppUtils.instance.filesDir
    }

    fun getExternalFilesDir(path: String?): File? {
        return AppUtils.instance.getExternalFilesDir(path)
    }

    fun getDimension(@DimenRes id: Int): Float {
        return AppUtils.instance.resources.getDimension(id)
    }

    /**
     * Return the id identifier by name.
     *
     * @param name The name of id.
     * @return the id identifier by name
     */
    fun getIdByName(name: String?): Int {
        return AppUtils.instance.resources.getIdentifier(name, "id", AppUtils.instance.packageName)
    }

    /**
     * Return the string identifier by name.
     *
     * @param name The name of string.
     * @return the string identifier by name
     */
    fun getStringIdByName(name: String?): Int {
        return AppUtils.instance.resources.getIdentifier(
            name,
            "string",
            AppUtils.instance.packageName
        )
    }

    /**
     * Return the color identifier by name.
     *
     * @param name The name of color.
     * @return the color identifier by name
     */
    fun getColorIdByName(name: String?): Int {
        return AppUtils.instance.resources.getIdentifier(name, "color", AppUtils.instance.packageName)
    }

    /**
     * Return the dimen identifier by name.
     *
     * @param name The name of dimen.
     * @return the dimen identifier by name
     */
    fun getDimenIdByName(name: String?): Int {
        return AppUtils.instance.resources.getIdentifier(name, "dimen", AppUtils.instance.packageName)
    }

    /**
     * Return the drawable identifier by name.
     *
     * @param name The name of drawable.
     * @return the drawable identifier by name
     */
    fun getDrawableIdByName(name: String?): Int {
        return AppUtils.instance.resources.getIdentifier(
            name,
            "drawable",
            AppUtils.instance.packageName
        )
    }

    /**
     * Return the mipmap identifier by name.
     *
     * @param name The name of mipmap.
     * @return the mipmap identifier by name
     */
    fun getMipmapIdByName(name: String?): Int {
        return AppUtils.instance.resources.getIdentifier(
            name,
            "mipmap",
            AppUtils.instance.packageName
        )
    }

    /**
     * Return the layout identifier by name.
     *
     * @param name The name of layout.
     * @return the layout identifier by name
     */
    fun getLayoutIdByName(name: String?): Int {
        return AppUtils.instance.resources.getIdentifier(
            name,
            "layout",
            AppUtils.instance.packageName
        )
    }

    /**
     * Return the style identifier by name.
     *
     * @param name The name of style.
     * @return the style identifier by name
     */
    fun getStyleIdByName(name: String?): Int {
        return AppUtils.instance.resources.getIdentifier(name, "style", AppUtils.instance.packageName)
    }

    /**
     * Return the anim identifier by name.
     *
     * @param name The name of anim.
     * @return the anim identifier by name
     */
    fun getAnimIdByName(name: String?): Int {
        return AppUtils.instance.resources.getIdentifier(name, "anim", AppUtils.instance.packageName)
    }

    /**
     * Return the menu identifier by name.
     *
     * @param name The name of menu.
     * @return the menu identifier by name
     */
    fun getMenuIdByName(name: String?): Int {
        return AppUtils.instance.resources.getIdentifier(name, "menu", AppUtils.instance.packageName)
    }
}